package com.example.secondhandspringboot.service.impl;

import com.example.secondhandspringboot.constant.UserConstant;
import com.example.secondhandspringboot.domain.entity.User;
import com.example.secondhandspringboot.domain.vo.UserVO;
import com.example.secondhandspringboot.mapper.UserMapper;
import com.example.secondhandspringboot.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.secondhandspringboot.utils.Md5Util;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周在林
 * @since 2024-03-30
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final UserMapper userMapper;

    @Override
    public void register(String email, String password, Long schoolId) {

        User user = new User();
        user.setEmail(email);
        user.setUsername(UserConstant.USERNAME_PRE+ UUID.randomUUID().toString().substring(0,8));
        user.setPassword(Md5Util.getMD5String(password));
        user.setSchoolId(schoolId);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        this.save(user);
    }

    @Override
    public UserVO getUserVOByUserId(Long userId) {
        return userMapper.getUserVOByUserId(userId);
    }
}
